﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Core
{
    public static partial class contest
    {
        public static dynamic addOrEditProblem(int cid,char ps,
            int? oldPid, int pid, string name, int timeLimit, int memoryLimit, bool disabled,
            string description, string input, string output, string sampleInput, string sampleOutput, string source,
            int? exampleLang, string exampleCode, string inputData, string outputData,
            bool specialJudge, int? specialLang, string specialCode,string hint)
        {
            var sql = new Sql.SqlDataContext();

            Sql.ContestProblem p;
            if (oldPid == null) // Add
            {
                p = sql.ContestProblem.FirstOrDefault(row => row.pid == pid);
                if (p != null) return new
                {
                    success = false,
                    msg = "PID已存在"
                };

                Sql.ContestProblemStatistics cps = new Sql.ContestProblemStatistics();
                cps.accept = cps.submit = 0;
                cps.cid = cid;
                cps.ps = ps;
                sql.ContestProblemStatistics.InsertOnSubmit(cps);
            }
            else // Modify
            {
                p = sql.ContestProblem.FirstOrDefault(row => row.pid == oldPid);
                if (p == null) return new
                {
                    success = false,
                    msg = "题目不存在"
                };
            }
            if(p == null) p = new Sql.ContestProblem();
            p.cid = cid;
            p.pid = pid;
            p.ps = ps;
            p.name = name;
            p.timeLimit = timeLimit;
            p.memoryLimit = memoryLimit;
            p.description = description;
            p.input = input;
            p.output = output;
            p.sampleInput = sampleInput;
            p.sampleOutput = sampleOutput;
            p.source = source;
            p.exampleLang = exampleLang;
            p.exampleCode = exampleCode;
            p.inputData = inputData;
            p.outputData = outputData;
            p.specialJudge = specialJudge;
            p.specialLang = specialLang;
            p.specialCode = specialCode;
            p.disabled = disabled;
            p.hint = hint;
            if (oldPid == null) sql.ContestProblem.InsertOnSubmit(p);
            sql.SubmitChanges();
            return new
            {
                success = true
            };
        }
    }
}
